﻿var my_medias = [];
var com_media= null;
var src="";
var my_media=null;
var mediaTimer = null;var mediaTimerRecord = null;

    function captureSuccess(mediaFiles) {
		if(mediaFiles[0]!= null)
			navigator.notification.alert('co data');
		navigator.notification.alert('ko co data');
		my_media = mediaFiles[0];
    }
    function captureError(error) {
        var msg = 'An error occurred during capture: ' + error.code;
        navigator.notification.alert(msg, null, 'Uh oh!');
    }
    function captureAudio() {
        // Launch device audio recording application,
        // allowing user to capture up to 2 audio clips
        navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 1});
    }

function recordSound() {
	//my_media= null;
    //var d = new Date().getTime();
	src = "myrecord.wav";
	 var my_media = new Media(src, onSuccess, onError);
    // Record audio
    my_media.startRecord();
    // Stop recording after 5 sec
    var recTime = 0;
    var recInterval = setInterval(function () {
        recTime = recTime + 1;
        setAudioPosition(recTime + "s");
        if (recTime >= 5) {
            clearInterval(recInterval);
			console.log("----------------------"+ src);
            my_media.stopRecord();
			setAudioPosition("");
        }
    }, 1000);
}
function stopRecord() {
    setAudioPosition("stoped");
    my_media.stopRecord();
}
function playRecord() {
	console.log("----------------------"+ src);
	my_media = new Media("/sdcard/" + "myrecord.wav",onSuccess, onError);
    my_media.play();
    //if (mediaTimerRecord == null) {
        mediaTimerRecord = setInterval(function () {
            // get my_media position
            my_media.getCurrentPosition(
            // success callback
            function (position) {
                if (position >= 0) {
                    setUserAudioPosition(Math.floor(position) + "s");
                }
				if(position >= 5){setAudioPosition("");}
            },
            // error callback
            function (e) {
				setUserAudioPosition("0s");
            }
            );
        }, 1000);
    //}
}

function playAudio(src) {
    // Create Media object from src
    com_media = new Media(src, onSuccess, onError);

    // Play audio
    com_media.play();

    // Update my_media position every second
    if (mediaTimer == null) {
        mediaTimer = setInterval(function () {
            // get my_media position
            com_media.getCurrentPosition(
            // success callback
            function (position) {
                if (position > -1) {
                    setAudioPosition(Math.floor(position) + " sec");
                }
            },
            // error callback
            function (e) {
                console.log("Error getting pos=" + e);
                setAudioPosition("Error: " + e);
            }
            );
        }, 1000);
    }
}

function pauseAudio() {
    if (com_media) {
        com_media.pause();
    }
}

function stopAudio() {
    if (com_media) {
        com_media.stop();
    }
    clearInterval(mediaTimer);
    mediaTimer = null;
}

// onSuccess Callback
//
function onSuccess() {
    //console.log("playAudio():Audio Success");
}

// onError Callback
//
function onError(error) {

}

// Set audio position
//
function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}
function setUserAudioPosition(position) {
    document.getElementById('user_position').innerHTML = position;
}